//ajaxHTTP发送签入请求
var c;
// 保持心跳定时任务
var beat;



function LogIn() {
    //$("#mid").val("已发送请求......");
    var CMD = "LogInOrOut";
    var num;
    jQuery.support.cors = true;
    $.ajax({
        type: "post",
        url: url + "/LogInOrOut",
        data: {
            WorkNumber: strFJH,
            LoginType: 1,
            WorkGroup: strWorkerGroup,
            WorkID: strGH,
            PassWord: strPWD,
            dvType: 1,
            StateAcType: 1,
            AfertWorkTimeOut : afterWorkTimeOut,
            WorkState : workstate
        },
        cache:false ,
        dataType: "json",
        async: true,//加上之后不在跳转进error
        success: function (result) {
            // beat = window.setInterval('LogInON()', 15000);
        }
    });
    //function eventUsrLogin(vStrQueueName, vStrUsrName, vStrUsrPwd, vStrCaller, vIntResult, vStrMsg)
    return num;
}

//ajaxHTTP发送签入请求
function LogInON() {
    //$("#mid").val("已发送请求......");
    var CMD = "LogInOrOut";
    var num;
    $.ajax({
        type: "post",
        url: url + "/LogInOrOut",
        data: {
            CMD: CMD,
            WorkNumber: strFJH,
            LoginType: 2,
            WorkGroup: strWorkerGroup,
            WorkID: strGH,
            PassWord: strPWD,
            dvType: 1,
            StateAcType: 1,
            AfertWorkTimeOut : afterWorkTimeOut,
            WorkState : userStatus
        },
        dataType: "json",
        async: false, //加上之后不在跳转进error
        success: function (result) {
            console.log( "心跳保持");
            if (result["retCode"] === 1) {
                num = 1;
            } else {
                num = 0;
            }
        },
    });
    return num;
}

//ajaxHTTP发送签出请求
function LogOut() {
    //$("#mid").val("已发送请求......");
    var CMD = "LogInOrOut";
    var num;
    $.ajax({
        type: "post",
        url: url + "/LogInOrOut",
        data: {
            CMD: CMD, WorkNumber: strFJH, LoginType: 0
            , WorkGroup: strWorkerGroup, WorkID: strGH
            , PassWord: strPWD, dvType: 1, StateAcType: 1
			,AfertWorkTimeOut : afterWorkTimeOut
        },
        dataType: "json",
        async: false, //加上之后不在跳转进error
        success: function (result) {
            console.log( "清除心跳")
            window.clearInterval(c);
            /*if (result["retCode"] === 1) {
                num = 1;

                //window.setTimeout("eventUsrLogin('vStrQueueName',strGH,strPWD,'15047177101',1,'成功')", 1);
                //eventUsrLogout(vStrQueueName, vStrUsrName, vStrUsrPwd, vStrCaller, vIntResult);
            } else {
                num = 0;
                eventErrorReason(result["Exception"])
                //window.setTimeout("eventUsrLogin('vStrQueueName',strGH,strPWD,'15047177101',0,'失败')", 1);
            }*/
        },
    });
    return num;
}

//UpdateAgentState HTTP状态修改事件
function UpdateAgentState(Zt) {
    //$("#mid").val("已发送请求......");
    var CMD = "UpdateAgentState";
    var num;
    $.ajax({
        type: "post",
        url: url + "/UpdateAgentState",
        data: {
            CMD: CMD, WorkGroup: strWorkerGroup, WorkID: strGH, WorkState: Zt, WorkNumber: strFJH,WorkTimeOut: WorkTimeOut
        },
        dataType: "json",
        async: false, //加上之后不在跳转进error
        success: function (result) {
            if (result["retCode"] === 1) {
                num = 1;
            } else {
                num = 0;
                //eventErrorReason(result["Exception"])
            }
        },
    });
    return num;
}//UpdateAgentState HTTP状态修改事件
function SetIdle (Zt) {
    //$("#mid").val("已发送请求......");
    var CMD = "SetIdle";
    var num;
    $.ajax({
        type: "post",
        url: url + "/SetIdle",
        data: {
            CMD: CMD, WorkGroup: strWorkerGroup, WorkID: strGH, WorkState: Zt, WorkNumber: strFJH
        },
        dataType: "json",
        async: false, //加上之后不在跳转进error
        success: function (result) {
            if (result["retCode"] === 1) {
                num = 1;
            } else {
                num = 0;
                //eventErrorReason(result["Exception"])
            }
        },
    });
    return num;
}
function SetBusy (Zt) {
    //$("#mid").val("已发送请求......");
    var CMD = "SetIdle";
    var num;
    $.ajax({
        type: "post",
        url: url + "/SetIdle",
        data: {
            CMD: CMD, WorkGroup: strWorkerGroup, WorkID: strGH, WorkState: Zt, WorkNumber: strFJH
        },
        dataType: "json",
        async: false, //加上之后不在跳转进error
        success: function (result) {
            if (result["retCode"] === 1) {
                num = 1;
            } else {
                num = 0;
                //eventErrorReason(result["Exception"])
            }
        },
    });
    return num;
}

//MakeCall  外呼
function MakeCall(callee, worknumber) {
    //$("#mid").val("已发送请求......");
    var CMD = "MakeCall";
    var num;
    $.ajax({
        type: "post",
        url: url + "/MakeCall",
        data: {
            CMD: CMD, WorkGroup: strWorkerGroup, CallSerialNumber: "", //WorkState : Zt,
            Callee: callee,
            WorkNumber: worknumber,
            FlowID: FlowID
            , CallType: 1,
            CallHold: 60,
            DisCaller:strShowNum,
            FeeCallee: ""
        },
        dataType: "json",
        async: false, //加上之后不在跳转进error
        success: function (result) {
            if (result["retCode"] === 1) {
                num = 1;
            } else {
                num = 0;
                eventErrorReason(result["Exception"])
            }
        },
    });
    return num;
}

//主动挂机
function HangUp(worknumber) {
    //$("#mid").val("已发送请求......");
    var CMD = "HangUp";
    var num;
    $.ajax({
        type: "post",
        url: url + "/HangUp",
        data: {
            CMD: CMD, CallSerialNumber: CallSerialNumber, //WorkState : Zt,
            WorkNumber: worknumber
        },
        dataType: "json",
        async: false, //加上之后不在跳转进error
        success: function (result) {
            if (result["retCode"] === 1) {
                num = 1;
            } else {
                num = 0;
                //eventErrorReason(result["Exception"])
            }
        },
    });
    return num;
}//接听
function Answer(worknumber) {
    //$("#mid").val("已发送请求......");
    var CMD = "Answer";
    var num;
    $.ajax({
        type: "post",
        url: url + "/Answer",
        data: {
            CMD: CMD, CallSerialNumber: CallSerialNumber, //WorkState : Zt,
            WorkNumber: worknumber
        },
        dataType: "json",
        async: false, //加上之后不在跳转进error
        success: function (result) {
            if (result["retCode"] === 1) {
                num = 1;
            } else {
                num = 0;
                //eventErrorReason(result["Exception"])
            }
        },
    });
    return num;
}
//接听话机版本 ： 方位
function AnswerFangWei() {
    //$("#mid").val("已发送请求......");
    var num;
    jQuery.support.cors = true;
    $.ajax({
        type: "get",
        url: "http://admin:zkxb2005@192.168.3.39/cgi-bin/ConfigManApp.com?key=F_ACCEPT",
        //async: false, //加上之后不在跳转进error
        success: function (result) {
            obj2string(str)(result);
            if (result["retCode"] === 1) {
                num = 1;
            } else {
                num = 0;
                //eventErrorReason(result["Exception"])
            }
        },
    });
    return num;
}

//Start3Conf 三方会议
function Start3Conf(Callee, worknumber, callSerialNumber, vStrXSCaller) {
    //$("#mid").val("已发送请求......");
    var CMD = "Start3Conf";
    var num;
    $.ajax({
        type: "post",
        url: url+"/Start3Conf",
        data: {
            CMD: CMD,
            CallSerialNumber: callSerialNumber, //WorkState : Zt,
            WorkNumber: worknumber,
            Callee: Callee,
            CallHold: 60,
            DisCaller: strShowNum,
            FeeCallee: ""
        },
        dataType: "json",
        async: false, //加上之后不在跳转进error
        success: function (result) {
            if (result["retCode"] === 1) {
                num = 1;
            } else {
                num = 0;
                eventErrorReason(result["Exception"])
            }
        },
    });
    return num;
}

//TransferCall
function TransferCall(Callee, worknumber, callserial) {
    //$("#mid").val("已发送请求......");
    var CMD = "TransferCall";
    var num;
    $.ajax({
        type: "post",
        url: url+"/TransferCall",
        data: {
            CMD: CMD,
            CallSerialNumber: callserial, //WorkState : Zt,
            WorkNumber: worknumber,
            TraCallee: Callee,
            TraType: 1,
            CallHold: 30,
            DisCaller:strShowNum,
            FeeCallee: ""
        },
        dataType: "json",
        async: false, //加上之后不在跳转进error
        success: function (result) {
           log(result)
            if (result["retCode"] === 1) {
                num = 1;
            } else {
                num = 0;
                eventErrorReason(result["Exception"])
            }
        },
    });
    return num;
}//AgentListen 坐席监听
function AgentListen(Callee, worknumber) {
    //$("#mid").val("已发送请求......");
    var CMD = "AgentListen";
    var num;
    $.ajax({
        type: "post",
        url: url+"/AgentListen",
        data: {
            CMD: CMD,
            CallSerialNumber: CallSerialNumber, //WorkState : Zt,
            WorkNumber: worknumber,
            Callee: Callee,
            //TraType : 2,
            CallHold: 60,
            DisCaller: strShowNum,
            FeeCallee: ""
        },
        dataType: "json",
        async: false, //加上之后不在跳转进error
        success: function (result) {

            if (result["retCode"] === 1) {
                num = 1;
            } else {
                num = 0;
                eventErrorReason(result["Exception"])
            }
        },
    });
    return num;
}

//获取分机事件
function getAgentEvent(worknumber) {
    //$("#mid").val("已发送请求......");
    var CMD = CMD || "getAgentEvent";
    var num;

    $.ajax({
        type: "post",
        url: statementUrl+ "/getAgentEvent",
        data: {
                CMD: CMD,
                WorkNumber: worknumber,
        },
        cache:false ,
        async: false,
        dataType: "json",
         //加上之后不在跳转进error
        success: function (result) {
            var json;
            if(window.execScript){
               json = result;
            }else {
                //非IE浏览器
                //json  =  $.parseJSON(dataJson);
				if(json!=null){
					json =  window.eval("("+result+")");
				}else{
					json = result
				}
            }
            if(baseUrl === 'http://114.115.216.86:8081'){
                json = $.parseJSON( json )
            }
          //eval("("+result+")");
           num =json
        },
        error:function () {
            alert("服务请求失败！ 请确保网络通畅 如需帮助请联系管理人员")
        }
    });

    return num;
}
//获取分机呼出明细
function getCallOutLogByID(worknumber, date) {
    var myDate = new Date();
    var tYear = myDate.getFullYear();
    var tMonth = myDate.getMonth() + 1;
    var yearMonth = date || tYear + ('0' + tMonth).substr(-2);

    var CMD = "getCallOutLogByID";
    var callData;

    console.log('获取分机呼出明细相关参数：', 'CMD: ' + CMD, 'yearMonth: ' + yearMonth, 'worknumber: ' + worknumber);

    $.ajax({
        type: "post",
        url: baseUrl + "/jncdr/queryHandler/",
        data: {
            CMD: CMD,
            YearMonth: yearMonth,
            CallID: worknumber,
        },
        cache:false ,
        async: false,
        dataType: "json",
        //加上之后不在跳转进error
        success: function (result) {
            callData = result;
            console.log(callData)
        },
        error:function (err) {
            console.log(err);
        }
    })
    return callData;
}

function log(msg){
    if (window["console"]){
        console.log(msg);
    }
}



function Hold(WorkGroup, holdType) {
    //$("#mid").val("已发送请求......");
    var CMD = "Hold";
    var num;
    $.ajax({
        type: "post",
        url: url +"/Hold",
        data: {
            CMD: CMD,
           //WorkState : Zt,
            WorkGroup: WorkGroup,
            HoldType: holdType,
            WorkNumber: strFJH ,
            CallSerialNumber: CallSerialNumber,
        },
        dataType: "json",
        async: false, //加上之后不在跳转进error
        success: function (result) {
            if (result["retCode"] === 1) {
                num = 1;
            } else {
                num = 0;
                eventErrorReason(result["Exception"])
            }
        },
    });
    return num;

}

function getUserQueueInfo(WorkGroup) {
    //$("#mid").val("已发送请求......");
    var CMD = "getUserQueueInfo";
    var num;
    $.ajax({
        type: "post",
        url: url+"/getUserQueueInfo",
        data: {
            CMD: CMD,
            //CallSerialNumber : "", //WorkState : Zt,
            WorkGroup: WorkGroup,
        },
        dataType: "json",
        async: false, //加上之后不在跳转进error
        success: function (result) {
            if (result["UserCount"] != null) {
                num = result["UserCount"];
            } else {
                num = 0;
                //eventErrorReason(result["Exception"])
            }
        },
    });
    return num;

}


// 获取中间件平台实时并发数据
function getSystemOnlineInfo() {
    $.ajax({
        type: "post",
        url: baseUrl + "/jncdr/jntel/GetSystemOnlineInfo",
        cache:false ,
        async: false,
        dataType: "json",
        //加上之后不在跳转进error
        success: function (result) {
            callData = result;
            console.log(callData);
        },
        error:function (err) {
            console.log(err);
        }
    })
    return callData;
}

// 获取当天呼叫信息
function getAllCallSumCount() {
    $.ajax({
        type: "post",
        url: baseUrl + "/jncdr/jncdr/getAllCallSumCount",
        data: {
            TjUser: 'admin',
            sign: 'admin',
            DayReport: '20210517',
        },
        cache:false ,
        async: false,
        dataType: "json",
        success: function (result) {
            callData = result;
            console.log(callData);
        },
        error:function (err) {
            console.log(err);
        }
    })
    return callData;
}

// 获取分机坐席状态
function getAgentInfo() {
    $.ajax({
        type: "post",
        url: baseUrl + "/jncdr/jntel/getAgentInfo",
        data: {
            WorkNumber: strFJH,
            sign: '',
        },
        cache:false ,
        async: false,
        dataType: "json",
        success: function (result) {
            callData = result;
            console.log(callData);
        },
        error:function (err) {
            console.log(err);
        }
    })
    return callData;
}
